var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    markers: [{
      iconPath: "/images/nav/map.png",
      id: 0,
      latitude: 0,
      longitude:0,
      width: 18,
      height: 28
    }],
    pics:[],
    picimg:"",
    address:"",
    companyInfo: {
      StoreId: 0, Uid: 0, Name: "", Phone: "", Banner: "",Description: "",Address: "",Location: ""//小的在前面           
    },
    enablescroll:false, //禁止地图拖动
    cardId: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getUserStoreInfo();
  },

  //获取公司名称
  getCompany:function(e){
    var companyInfo = this.data.companyInfo;
    companyInfo.Name = e.detail.value;
    this.setData({ companyInfo: companyInfo})
  },

  //获取公司介绍
  getCompanyDesc: function (e) {
    var companyInfo = this.data.companyInfo;
    companyInfo.Description = e.detail.value;
    this.setData({ companyInfo: companyInfo })
  },

  //获取公司电话
  getCompanyPhone: function (e) {
    var companyInfo = this.data.companyInfo;
    companyInfo.Phone = e.detail.value;
    this.setData({ companyInfo: companyInfo })
  },

  //获取公司地址
  getCompanyAderess: function (e) {
    var companyInfo = this.data.companyInfo;
    companyInfo.Address = e.detail.value;
    this.setData({ companyInfo: companyInfo })
  },

  //获取公司视频地址
  getCompanyVideoSrc:function(e){
    var companyInfo = this.data.companyInfo;
    companyInfo.VideoSrc = e.detail.value;
    this.setData({ companyInfo: companyInfo })
  },

  //获取公司视频地址（修改vid）
  getCompanyVideoSrc2: function (e) {
    var companyInfo = this.data.companyInfo;
    companyInfo.VID = e.detail.value;
    this.setData({ companyInfo: companyInfo })
  },

  //提交产品官网
  updateUserStoreInfo:function(){
    wx.showLoading({
      title: '提交中...',
      mask: true,
    })
    var that = this;
    var companyInfo = that.data.companyInfo;
    var markers = that.data.markers;
    if (that.data.picimg){
      companyInfo.Banner = that.data.picimg;
    }else{
      var banner = companyInfo.Banner;
      var index = banner.lastIndexOf("\/");
      banner = banner.substring(index + 1, banner.length);
      companyInfo.Banner = banner;
    }
    if (!companyInfo.Address){
      companyInfo.Address = that.data.address;
    }
    var locations = markers[0].latitude + "," + markers[0].longitude;
    companyInfo.Location = locations;
    companyInfo.StoreId = app.globalData.storeId;
    companyInfo.Uid = app.globalData.userInfo.Uid; 
    companyInfo.VideoSrc = encodeURIComponent(companyInfo.VideoSrc);
    if (companyInfo.VideoSrc){
      companyInfo.VID = '';
    }
    if (companyInfo.VID.indexOf('https') != -1){
      companyInfo.VideoSrc = encodeURIComponent(companyInfo.VID);
    }
    else{
      companyInfo.VID = companyInfo.VID;
    }
    wx.request({
      url: app.globalData.host + '/api/Store/UpdateUserStoreInfo',
      data: companyInfo,
      method: 'POST',
      success: function(res) {
        wx.hideLoading();
        if(res.data.Code == 0){
          wx.showToast({
            title: '提交成功！',
          })
          wx.navigateBack({
            delta: 1,
          })
        }else{
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            cancel:false
          })
        }
      },
    })
  },

  //获取我的官网信息
  getUserStoreInfo:function(){
    var that = this;
    var cardId = wx.getStorageSync("cardId");
    var companyInfo = that.data.companyInfo;
    var markers = that.data.markers;
    wx.request({
      url: app.globalData.host + '/api/Store/GetUserStoreInfo?uid=' + app.globalData.userInfo.Uid,
      success: function(res) {
        if(res.data.Code == 0){
          var data = res.data.Data;
          companyInfo.Name = data.Name;
          companyInfo.Description = data.Description;
          companyInfo.Banner = 'https://static.711688.net.cn/upload/user/source/' + data.Banner;
          companyInfo.Phone = data.Phone;
          companyInfo.Address = data.Address;
          companyInfo.Location = data.Location;
          companyInfo.VideoSrc = data.VideoSrc;
          companyInfo.VID = data.VID;
          var lats = data.Location.split(",");
          markers[0].latitude = lats[0];
          markers[0].longitude = lats[1];
          that.setData({ companyInfo: companyInfo, markers: markers,cardId: cardId})
        }
      },
    })
  },

  //获取地理位置
  getMapLocaltion: function (e) {
    var that = this;
    var companyInfo = that.data.companyInfo;
    var cardId = e.currentTarget.dataset.cardid;
    wx.chooseLocation({
      success: function (res) {
        wx.setStorageSync("cardId", cardId);
        var markers = that.data.markers;
        markers[0].latitude = res.latitude;
        markers[0].longitude = res.longitude;
        companyInfo.Address = res.address
        that.setData({
          address: res.address,
          markers: markers,
          companyInfo: companyInfo
        })
      },
      fail: function () {
        wx.getSetting({
          success(res) {
            if (!res.authSetting['scope.userLocation']) {
              wx.authorize({
                scope: 'scope.userLocation',
                success(res) {
                  wx.chooseLocation({
                    success: function (res) {
                      var markers = that.data.markers;
                      markers[0].latitude = res.latitude;
                      markers[0].longitude = res.longitude;
                      companyInfo.Address = res.address
                      that.setData({
                        address: res.address,
                        markers: markers,
                        companyInfo: companyInfo
                      })
                    },
                  })
                }
              })
            }
          }
        })
      }
    })
  },
  

  chooseUploadImg: function (e) { //这里是选取图片的方法
    var that = this;
    wx.chooseImage({
      count: 1, // 最多可以选择的图片张数，默认9
      sizeType: ['original','compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        var imgsrc = [];
        imgsrc.push(res.tempFilePaths[0]);
        that.setData({ pics: imgsrc });
      },
    })

  },

  companyUploadImg: function () { //这里触发图片上传的方法
    var that = this;
    var pics = that.data.pics;
    if (pics.length == 0){
      that.updateUserStoreInfo()
    }else{
      that.uploadimg({
        url: app.globalData.host + '/api/tool/upload?operation=uploaduserreview', //这里是你图片上传的接口
        path: pics, //这里是选取的图片的地址数组
      });
    }
  },

  //多张图片上传
  uploadimg: function (data) {
    wx.showLoading({
      title: '提交中...',
      mask: true,
    })
    var that = this, i = data.i ? data.i : 0, success = data.success ? data.success : 0, fail = data.fail ? data.fail : 0;
    wx.uploadFile({
      url: data.url,
      filePath: data.path[i],
      name: 'fileData',
      formData: null,
      success: (resp) => {
        wx.hideLoading();
        success++;
        var str = resp.data
        var pic = JSON.parse(str);
        var pic_name = pic.Data;
        that.setData({
          picimg: pic_name
        })
      },
      fail: (res) => {
        fail++;
        console.log('fail:' + i + "fail:" + fail);
      },
      complete: () => {
        i++;
        if (i == data.path.length) { //当图片传完时，停止调用     
          console.log('成功：' + success + " 失败：" + fail);
          that.updateUserStoreInfo();
        } else { //若图片还没有传完，则继续调用函数
          data.i = i;
          data.success = success;
          data.fail = fail;
          that.uploadimg(data);
        }
      }
    });
  }
})